package com.luoqiz.file.util;

import java.io.File;
import java.io.FileInputStream;

import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.Parser;
import org.apache.tika.sax.BodyContentHandler;
import org.xml.sax.ContentHandler;

/**
 * 判断文件类型，如doc和docx也能正确区分,推荐使用
 * 
 * 将文件系统上的文件获取了这些mimeType
gif : image/gif 
bmp : image/bmp 
ico : image/x-ico 
jpeg : image/jpeg 
jpg : image/jpeg 
png : image/png 
zip : application/zip 
rar : application/x-rar 
jsp : text/html 
pdf : application/pdf 
png : image/png 
jpg : image/jpeg 
mp4 : video/mp4 
flv : video/x-flv 
ppt : application/vnd.ms-powerpoint 
pptx : application/vnd.openxmlformats-officedocument.presentationml.presentation 
xlsx : application/vnd.openxmlformats-officedocument.spreadsheetml.sheet 
xls : application/vnd.ms-excel 
doc : application/msword 
docx : application/vnd.openxmlformats-officedocument.wordprocessingml.document 
txt : text/plain 


 * @author luoqiz
 *
 */
public class Main {

	public static void main(String args[]) throws Exception {

		FileInputStream is = null;
		try {
//			File f = new File("e:/123Test.docx");
//			File f = new File("e:/123.docx");
//			File f = new File("e:/1.doc");
			File f = new File("e:/2.docx");
			is = new FileInputStream(f);

			ContentHandler contenthandler = new BodyContentHandler();
			Metadata metadata = new Metadata();
			metadata.set(Metadata.RESOURCE_NAME_KEY, f.getName());
			Parser parser = new AutoDetectParser();
			// OOXMLParser parser = new OOXMLParser();
			ParseContext p = new ParseContext();
			parser.parse(is, contenthandler, metadata, p);
			System.out.println("Mime: " + metadata.get(Metadata.CONTENT_TYPE));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (is != null)
				is.close();
		}
	}
}